package com.student.Activitys.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.SparseIntArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.student.Activitys.R;
import com.student.Activitys.bean.MemberFamilyBean;
import com.student.Activitys.bean.UserBean;
import com.student.Activitys.bean.pack.PartyApplyPack;
import com.student.Activitys.view.PinnedHeaderExpandableListView;

import java.util.ArrayList;
import java.util.List;


/**
 * 项目名称：
 * 类描述：奖惩资助 适配器
 * 创建人：张飞祥
 * 创建时间：2017/9/8 17:06
 * 修改人：张飞祥
 * 修改时间：2017/9/8 17:06
 * 修改备注：
 */
public class PartyApplyExpandableAdapter extends BaseExpandableListAdapter implements PinnedHeaderExpandableListView.HeaderAdapter {

    private List<PartyApplyPack> childrenData;
    private Context context;
    private PinnedHeaderExpandableListView listView;
    private LayoutInflater inflater;
    //单个实体类
    private PartyApplyPack pack;
    //子布局 标记
    private int type;
    //个人信息 子布局 EditText 填写集合
    private List<EditText> userMsgs;
    //个人信息 子布局 EditText 填写集合
    private List<EditText> familys;

    public PartyApplyExpandableAdapter(List<PartyApplyPack> childrenData, Context context, PinnedHeaderExpandableListView listView) {
        this.childrenData = childrenData;
        this.context = context;
        this.listView = listView;
        inflater = LayoutInflater.from(this.context);
    }

    //返回子节点第i个
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return 0;
    }

    /**
     * 子节点 数据填充 根据 type 值 来决定加载那种布局
     *
     * @param groupPosition
     * @param childPosition
     * @param isLastChild
     * @param convertView
     * @param parent
     * @return
     */
    @Override
    public View getChildView(final int groupPosition, int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {
        View view = null;
        type = childrenData.get(groupPosition).getType();
        switch (type) {

            case 0:     //个人信息
                view = createChildrenView(R.layout.item_public_apply_my_msg);
                //名字
                TextView tvGroupMyName = (TextView) view.findViewById(R.id.tv_group_my_name);
                //性别
                TextView tvGroupMySex = (TextView) view.findViewById(R.id.tv_group_my_sex);
                //民族
                TextView tvGroupMyNation = (TextView) view.findViewById(R.id.tv_group_my_nation);
                //学号
                TextView tvGroupMyStudentId = (TextView) view.findViewById(R.id.tv_group_my_student_id);
                //班级
                TextView tvGroupMyClass = (TextView) view.findViewById(R.id.tv_group_my_class);
                //在读职位
                TextView tvGroupMyPosition = (TextView) view.findViewById(R.id.tv_group_my_position);
                //政治面貌
                TextView tvGroupMyPolitics = (TextView) view.findViewById(R.id.tv_group_my_politics);
                /**
                 * 个人信息 默认信息
                 */
                tvGroupMyName.setText(childrenData.get(groupPosition).getUser().getStuName());
                tvGroupMySex.setText(childrenData.get(groupPosition).getUser().getXB());
                tvGroupMyNation.setText(childrenData.get(groupPosition).getUser().getMZ());
                tvGroupMyStudentId.setText(childrenData.get(groupPosition).getUser().getXH());
                tvGroupMyClass.setText(childrenData.get(groupPosition).getUser().getXZBJ());
                tvGroupMyPosition.setText(childrenData.get(groupPosition).getUser().getZDZW());
                tvGroupMyPolitics.setText(childrenData.get(groupPosition).getUser().getZZMM());

                /**
                 * 个人信息 手动填写
                 */
                //身份证号
                 EditText evGroupMyId = (EditText) view.findViewById(R.id.ev_group_my_id);
                //手机号
                 EditText evGroupMyPhoneNumber = (EditText) view.findViewById(R.id.ev_group_my_phone_number);
                //邮箱地址
                 EditText evGroupMyEmail = (EditText) view.findViewById(R.id.ev_group_my_email);
                //QQ
                 EditText evGroupMyQQ = (EditText) view.findViewById(R.id.ev_group_my_qq);
                //微信
                 EditText evGroupMyWeixin = (EditText) view.findViewById(R.id.ev_group_my_weixin);
                //家庭住址
                 EditText evGroupMyAddress = (EditText) view.findViewById(R.id.ev_group_my_address);

                //控件集合
                userMsgs = new ArrayList<>();
                userMsgs.add(evGroupMyId);
                userMsgs.add(evGroupMyPhoneNumber);
                userMsgs.add(evGroupMyEmail);
                userMsgs.add(evGroupMyQQ);
                userMsgs.add(evGroupMyWeixin);
                userMsgs.add(evGroupMyAddress);

                for (int i = 0 ; i < userMsgs.size() ; i++){
                    UserBean user = childrenData.get(groupPosition).getUser();
                    switch (i){
                        case 0:         //身份证号
                            if (user!=null && user.getSFZH()!=null)
                                evGroupMyId.setText(user.getSFZH());
                            break;
                        case 1:         //手机号
                            if (user!=null && user.getMobile()!=null)
                                evGroupMyPhoneNumber.setText(user.getMobile());
                            break;
                        case 2:         //邮箱地址
                            if (user!=null && user.getMail()!=null)
                                evGroupMyEmail.setText(user.getMail());
                            break;
                        case 3:         //QQ
                            if (user!=null && user.getQQ()!=null)
                                evGroupMyQQ.setText(user.getQQ());
                            break;
                        case 4:         //微信号
                            if (user!=null && user.getWeixin()!=null)
                                evGroupMyWeixin.setText(user.getWeixin());
                            break;
                        case 5:         //家庭住址
                            if (user!=null && user.getFamilyAddr()!=null)
                                evGroupMyAddress.setText(user.getFamilyAddr());
                            break;
                    }
                    /**
                     *  关键就是这里的 "我的个人信息" (EditText)保存数据 事件
                     */
                    final int myMsgI = i;
                    userMsgs.get(i).addTextChangedListener(new TextWatcher() {
                        @Override
                        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                        }

                        @Override
                        public void onTextChanged(CharSequence s, int start, int before, int count) {
                            pack = childrenData.get(groupPosition);
                            switch (myMsgI){
                                case 0:         //身份证号
                                    pack.getUser().setSFZH(s.toString());            //更新 身份证号
                                    break;
                                case 1:         //手机号
                                    pack.getUser().setMobile(s.toString());          //更新 手机号
                                    break;
                                case 2:         //邮箱地址
                                    pack.getUser().setMail(s.toString());            //更新 邮箱地址
                                    break;
                                case 3:         //QQ
                                    pack.getUser().setQQ(s.toString());              //更新 QQ
                                    break;
                                case 4:         //微信号
                                    pack.getUser().setWeixin(s.toString());          //更新 微信号
                                    break;
                                case 5:         //家庭住址
                                    pack.getUser().setFamilyAddr(s.toString());      //更新 家庭住址
                                    break;
                            }
                            //每当内容改变时，就更新集合中的数据
                            childrenData.set(groupPosition, pack);
                        }
                        @Override
                        public void afterTextChanged(Editable s) {
                        }
                    });
                }

                break;
            case 1:     //家人信息
                view = createChildrenView(R.layout.item_public_apply_my_family);
                /**
                 * 我的家人信息
                 */
                //父亲名字
                EditText evGroupMyFatherName = (EditText) view.findViewById(R.id.ev_group_my_father_name);
                //父亲电话
                EditText evGroupMyfatherPhoneNumber = (EditText) view.findViewById(R.id.ev_group_my_father_phone_number);
                //父亲工作单位
                EditText evGroupMyFatherCompany = (EditText) view.findViewById(R.id.ev_group_my_father_company);
                //父亲职务
                EditText evGroupMyFatherPost = (EditText) view.findViewById(R.id.ev_group_my_father_post);
                //母亲名字
                EditText evGroupMyMotherName = (EditText) view.findViewById(R.id.ev_group_my_mother_name);
                //母亲电话
                EditText evGroupMyMotherPhoneNumber = (EditText) view.findViewById(R.id.ev_group_my_mother_phone_number);
                //母亲工作单位
                EditText evGroupMyMotherCompany = (EditText) view.findViewById(R.id.ev_group_my_mother_company);
                //母亲职务
                EditText evGroupMyMotherPost = (EditText) view.findViewById(R.id.ev_group_my_mother_post);

                //控件集合
                familys = new ArrayList<>();
                familys.add(evGroupMyFatherName);
                familys.add(evGroupMyfatherPhoneNumber);
                familys.add(evGroupMyFatherCompany);
                familys.add(evGroupMyFatherPost);
                familys.add(evGroupMyMotherName);
                familys.add(evGroupMyMotherPhoneNumber);
                familys.add(evGroupMyMotherCompany);
                familys.add(evGroupMyMotherPost);

                for (int i = 0 ; i < familys.size() ; i++){
                    MemberFamilyBean family = childrenData.get(groupPosition).getFamily();
                    switch (i){
                        case 0:         //父亲名字
                            if (family!=null && family.getFatherName()!=null)
                                evGroupMyFatherName.setText(family.getFatherName());
                            break;
                        case 1:         //父亲电话
                            if (family!=null && family.getFatherPhoneNumber()!=null)
                                evGroupMyfatherPhoneNumber.setText(family.getFatherPhoneNumber());
                            break;
                        case 2:         //父亲工作单位
                            if (family!=null && family.getFatherCompany()!=null)
                                evGroupMyFatherCompany.setText(family.getFatherCompany());
                            break;
                        case 3:         //父亲职务
                            if (family!=null && family.getFatherPost()!=null)
                                evGroupMyFatherPost.setText(family.getFatherPost());
                            break;
                        case 4:         //母亲名字
                            if (family!=null && family.getMotherName()!=null)
                                evGroupMyMotherName.setText(family.getMotherName());
                            break;
                        case 5:         //母亲电话
                            if (family!=null && family.getMotherPhoneNumber()!=null)
                                evGroupMyMotherPhoneNumber.setText(family.getMotherPhoneNumber());
                            break;
                        case 6:         //母亲工作单位
                            if (family!=null && family.getMotherCompany()!=null)
                                evGroupMyMotherCompany.setText(family.getMotherCompany());
                            break;
                        case 7:         //母亲职务
                            if (family!=null && family.getMotherPost()!=null)
                                evGroupMyMotherPost.setText(family.getMotherPost());
                            break;
                    }
                    /**
                     *  关键就是这里的 "我的家人信息" (EditText)保存数据 事件
                     */
                    final int familyI = i;
                    familys.get(i).addTextChangedListener(new TextWatcher() {
                        @Override
                        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                        }
                        @Override
                        public void onTextChanged(CharSequence s, int start, int before, int count) {
                            pack = childrenData.get(groupPosition);
                            switch (familyI){
                                case 0:         //父亲名字
                                    pack.getFamily().setFatherName(s.toString());            //更新 父亲名字
                                    break;
                                case 1:         //父亲电话
                                    pack.getFamily().setFatherPhoneNumber(s.toString());     //更新 父亲电话
                                    break;
                                case 2:         //父亲工作单位
                                    pack.getFamily().setFatherCompany(s.toString());         //更新 父亲工作单位
                                    break;
                                case 3:         //父亲职务
                                    pack.getFamily().setFatherPost(s.toString());            //更新 父亲职务
                                    break;
                                case 4:         //母亲姓名
                                    pack.getFamily().setMotherName(s.toString());            //更新 母亲姓名
                                    break;
                                case 5:         //母亲电话
                                    pack.getFamily().setMotherPhoneNumber(s.toString());     //更新 母亲电话
                                    break;
                                case 6:         //母亲工作单位
                                    pack.getFamily().setMotherCompany(s.toString());         //更新 母亲工作单位
                                    break;
                                case 7:         //母亲工作单位
                                    pack.getFamily().setMotherCompany(s.toString());         //更新 母亲工作单位
                                    break;
                            }
                            //每当内容改变时，就更新集合中的数据
                            childrenData.set(groupPosition, pack);
                        }
                        @Override
                        public void afterTextChanged(Editable s) {
                        }
                    });
                }
                break;
            default:    //入党志愿 && 本人经历
                view = createChildrenView(R.layout.item_public_apply_edit);
                //标注 文本信息
                TextView tvGroupLable = (TextView) view.findViewById(R.id.tv_group_label);
                //内容
                EditText etGroupContent = (EditText) view.findViewById(R.id.et_group_content);
                /**
                 * 填充（入党志愿 && 本人经历） 默认 & 动态 数据
                 */
                tvGroupLable.setText("（注：200字以内）");
                if (type == 2){
                    //入党志愿
                    etGroupContent.setHint("请填写您的入党志愿");
                    if (childrenData.get(groupPosition).getVolunteer()!=null)
                        etGroupContent.setText(childrenData.get(groupPosition).getVolunteer());
                }else{
                    //本人经历
                    etGroupContent.setHint("请填写您的个人经历");
                    if (childrenData.get(groupPosition).getExperience()!=null)
                        etGroupContent.setText(childrenData.get(groupPosition).getExperience());
                }

                /**
                 *  关键就是这里的 "入党志愿 && 本人经历" (EditText)保存数据 事件
                 */
                etGroupContent.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {
                        pack = childrenData.get(groupPosition);
                        if (type==2)
                            pack.setVolunteer(s.toString());        //入党志愿
                        else if (type==3)
                            pack.setExperience(s.toString());       //本人经历
                        //每当内容改变时，就更新集合中的数据
                        childrenData.set(groupPosition, pack);
                    }
                    @Override
                    public void afterTextChanged(Editable s) {
                    }
                });
                break;
        }
        return view;
    }

    //返回子节点 长度
    @Override
    public int getChildrenCount(int groupPosition) {
        return 1;
    }

    //返回父节点的 第i个
    @Override
    public Object getGroup(int groupPosition) {
        return childrenData.get(groupPosition);
    }

    //返回父节点的长度
    @Override
    public int getGroupCount() {
        return childrenData.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return 0;
    }

    /**
     * 父节点 数据填充
     *
     * @param groupPosition
     * @param isExpanded
     * @param convertView
     * @param parent
     * @return
     */
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        View view = null;
        if (convertView != null) {
            view = convertView;
        } else {
            view = createGroupView();
        }

        ImageView iv = (ImageView) view.findViewById(R.id.iv_group_icon);

        if (isExpanded) {
            iv.setImageResource(R.drawable.ic_group_on);
        } else {
            iv.setImageResource(R.drawable.ic_group_off);
        }

        TextView text = (TextView) view.findViewById(R.id.tv_group_title);
        text.setText(childrenData.get(groupPosition).getName());
        return view;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    private View createChildrenView(int layout) {
        return inflater.inflate(layout, null);
    }

    private View createGroupView() {
        return inflater.inflate(R.layout.item_public_group, null);
    }

    @Override
    public int getHeaderState(int groupPosition, int childPosition) {
        final int childCount = getChildrenCount(groupPosition);
        if (childPosition == childCount - 1) {
            return PINNED_HEADER_PUSHED_UP;
        } else if (childPosition == -1
                && !listView.isGroupExpanded(groupPosition)) {
            return PINNED_HEADER_GONE;
        } else {
            return PINNED_HEADER_VISIBLE;
        }
    }

    @Override
    public void configureHeader(View header, int groupPosition,
                                int childPosition, int alpha) {
        String groupData = this.childrenData.get(groupPosition).getName();
        ((TextView) header.findViewById(R.id.tv_group_title)).setText(groupData);

    }

    private SparseIntArray groupStatusMap = new SparseIntArray();

    @Override
    public void setGroupClickStatus(int groupPosition, int status) {
        groupStatusMap.put(groupPosition, status);
    }

    @Override
    public int getGroupClickStatus(int groupPosition) {
        if (groupStatusMap.keyAt(groupPosition) >= 0) {
            return groupStatusMap.get(groupPosition);
        } else {
            return 0;
        }
    }

    /**
     * 返回 当前数据源
     */
    public List<PartyApplyPack> getAdapterList(){
        return childrenData;
    }

}
